package com.example.superoa.controller;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.KqAttendance;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.pojos.KqOverwork;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.AttendService;
import com.example.superoa.model.service.DemoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class AttendController {
	@Autowired
	private AttendService a_biz;
	
	@Autowired
	private DemoService d_biz;
	//kq-clock table初始化
	@RequestMapping("select_kqattend")
	@ResponseBody
	public Map<String,Object> select_kqattend(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray json = a_biz.selectDAll();
		int i=1;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			obj.put("sortable", i);
			i++;
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}
	
	//kq-clock 搜索
	@RequestMapping("select_sousuo")
	@ResponseBody
	public Map<String,Object> select_sousuo(int id,Date kai,Date jie,int page,int rows){
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray json = a_biz.selectSousuo(kai,jie,id);
		int i=1;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			obj.put("sortable", i);
			i++;
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}
	//新增补卡申请记录，判断
	@RequestMapping("select-attend")
	@ResponseBody
	public String panduan(Date date,HttpSession session) {
		return 	a_biz.panduan(date,Integer.parseInt(session.getAttribute("sid").toString()));
	}
	//kq-attendance-shen  新增补卡记录
	@PostMapping("add_attendance")
	@ResponseBody
	public String add_attendance(Date aDate,Time aDao,Time aTui,String aWhy,HttpSession session) {
		KqAttendance attence=new KqAttendance();
		attence.setAId(a_biz.selectattendid()+1);
		attence.setADate(aDate);
		attence.setADao(aDao);
		attence.setATui(aTui);
		attence.setAWhy(aWhy);
		System.err.println("---"+aWhy);
		a_biz.add(attence,Integer.parseInt(session.getAttribute("sid").toString()));
		return "";
	}
	//kq-repair-assessor  查看
	@RequestMapping("kq-attendance-cha")
	public void cha(Integer id,Model model) {
		JSONObject attr=a_biz.selectID(id);
		Staff staff=d_biz.selectStaffid(attr.getInteger("a_id"));
		model.addAttribute("attr", attr);
		model.addAttribute("staff", staff);
		
	}
}
